package com.ruoyi.web.controller.system;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EntityDeviceMode;
import com.ruoyi.system.service.IEntityDeviceModeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 器材型号Controller
 * 
 * @author ruoyi
 * @date 2021-01-09
 */
@Controller
@RequestMapping("/system/mode")
public class EntityDeviceModeController extends BaseController
{
    private String prefix = "system/mode";

    @Autowired
    private IEntityDeviceModeService entityDeviceModeService;

    @RequiresPermissions("system:mode:view")
    @GetMapping()
    public String mode()
    {
        return prefix + "/mode";
    }

    /**
     * 查询器材型号列表
     */
    @RequiresPermissions("system:mode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EntityDeviceMode entityDeviceMode)
    {
        startPage();
        List<EntityDeviceMode> list = entityDeviceModeService.selectEntityDeviceModeList(entityDeviceMode);
        return getDataTable(list);
    }

    /**
     * 导出器材型号列表
     */
    @RequiresPermissions("system:mode:export")
    @Log(title = "导出器材型号列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EntityDeviceMode entityDeviceMode)
    {
        List<EntityDeviceMode> list = entityDeviceModeService.selectEntityDeviceModeList(entityDeviceMode);
        ExcelUtil<EntityDeviceMode> util = new ExcelUtil<EntityDeviceMode>(EntityDeviceMode.class);
        return util.exportExcel(list, "mode");
    }

    /**
     * 新增器材型号
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存器材型号
     */
    @RequiresPermissions("system:mode:add")
    @Log(title = "新增保存器材型号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(EntityDeviceMode entityDeviceMode)
    {
        return toAjax(entityDeviceModeService.insertEntityDeviceMode(entityDeviceMode));
    }

    /**
     * 修改器材型号
     */
    @GetMapping("/edit/{id}")
    @RepeatSubmit
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EntityDeviceMode entityDeviceMode = entityDeviceModeService.selectEntityDeviceModeById(id);
        mmap.put("entityDeviceMode", entityDeviceMode);
        return prefix + "/edit";
    }

    /**
     * 修改保存器材型号
     */
    @RequiresPermissions("system:mode:edit")
    @Log(title = "修改保存器材型号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(EntityDeviceMode entityDeviceMode)
    {
        return toAjax(entityDeviceModeService.updateEntityDeviceMode(entityDeviceMode));
    }

    /**
     * 删除器材型号
     */
    @RequiresPermissions("system:mode:remove")
    @Log(title = "删除器材型号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(entityDeviceModeService.deleteEntityDeviceModeByIds(ids));
    }

    /**
     * * 检测器材型号名称是否存在
     */
    @PostMapping( "/checkModeName")
    @ResponseBody
    @RepeatSubmit
    public Boolean checkModeName(String modeName,Long id) {
        Long aLong = entityDeviceModeService.checkModeName(modeName,id);
        return 0==aLong?true:false;
    }

    @GetMapping("/queryValidModeByParam")
    @ResponseBody
    public AjaxResult queryValidModeByParam(String modeName,Long typeId){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
//       List<EntityCustomer> result = Collections.emptyList();
//        if(StringUtils.isEmpty(param)){
//            ajax.put("value", result);
//        }else{
//
//        }
        ajax.put("value", entityDeviceModeService.queryValidModeByParam(modeName,typeId));
        return ajax;
    }
}
